package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.VO.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lsx
 * @since 2021-03-01
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService ;
    //用户登录
    @PostMapping("login")
    public R  login(@RequestBody UcenterMember ucenterMember){
        String tocken = ucenterMemberService.login(ucenterMember);
        return R.ok().data("tocken",tocken);
    }
    //用户注册
    @PostMapping("register")
    public  R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return  R.ok();
    }
    //根据tocken获取用户信息
    @GetMapping("getMemberInfo")
    public  R  getMemberInfo(HttpServletRequest request){//参数为HttpServletRequest request 前端会将tocken 信息放到requet中
        //使用jwt工具类 得到用户id
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户id查询  得到user对象
        UcenterMember member = ucenterMemberService.getById(userId);

        return R.ok().data("userInfo",member);
    }
}

